package fit.deni.utils;

import java.math.BigInteger;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

import org.apache.tapestry5.ioc.annotations.Inject;

import fit.deni.beans.Log;
import fit.deni.beans.Zaposleni;
import fit.deni.hibernate.interfaces.LogService;

public class Utils {
	
	@Inject
	private static LogService logService;
	
	public static int getHashOf(String key) {
		try {
			MessageDigest digest = MessageDigest.getInstance("MD5");
			digest.reset();
			digest.update(key.getBytes(Charset.forName("UTF-8")));
			byte[] pass = digest.digest();
			return new BigInteger(pass).hashCode();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			return -1;
		}
		return -1;
	}
	
	public static int daysBetween(Date d1, Date d2) {
		return (int)( (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
	}
	
	public static Log createLog(String message) {
		Log log = new Log();
		log.setVrijeme(new Date(System.currentTimeMillis()));
		log.setLog(message);
		return log;
	}
	
	public static Log createPersonalisedLog(Zaposleni z, String message) {
		Log log = new Log();
		log.setVrijeme(new Date(System.currentTimeMillis()));
		log.setLog("User " + z.getId() + ": " + z.getUsername() + " " + message);
		return log;
	}

}
